Problem/Motivation

If I have a required number (integer) field and another button that triggers an AJAX call (e.g. 'Add another item'), A fatal error happens as long as the number field is empty, and this error is also visible in the console.

Argument 1 passed to Drupal\Core\Form\FormState::setError() must be of the type array, null given, called in /home/drupal/drupal-core/core/lib/Drupal/Core/Field/WidgetBase.php on line 446.

There's also a notice in watchdog:

Notice: Undefined index: value in Drupal\Core\Field\Plugin\Field\FieldWidget\NumberWidget->errorElement() (line 119

To reproduce with just core:

  • Add a required number field
  • Add a text field with unlimited cardinality
  • Go to the node form and click 'Add another item' without filling in any value

User interface changes

None

API changes

None

Data model changes

None

-----------------------------

Original Report

To replicate

  1. Install fresh d8.
  2. Install entity_browser and entity_browser_example (submodule).
  3. On the provided Entity browser test content type, add a required Number (integer) field that allows 1 value.
  4. To see more useful debugging info, disable CSS/JS aggregation. (optional)
  5. Open your browser console.
  6. Create content of type Entity browser test. To attempt to bring up the modal window, open the second Files fieldset and click the button Select entities. Note that the expected modal does not appear, and errors are thrown in the browser console:
    Uncaught AjaxError:
    An AJAX HTTP error occurred.
    HTTP Result Code: 200
    Debugging information follows.
    Path: /node/add/entity_browser_test?ajax_form=1
    StatusText: OK
    ResponseText: Drupal.Ajax.error @ ajax.js?v=8.0.0-rc3:815ajax.options.complete @ ajax.js?v=8.0.0-rc3:419t.complete @ jquery.form.min.js?v=3.51:11j @ jquery.js:3099k.fireWith @ jquery.js:3211x @ jquery.js:8279(anonymous function) @ jquery.js:8605
  7. Enter an integer into your number field.
  8. Click the Select entities button again. Now it works!

Comments

jfrederick created an issue. See original summary.

jfrederick’s picture

Status: Active » Needs review
StatusFileSize
new660 bytes

And a patch.

amateescu’s picture

Status: Needs review » Postponed (maintainer needs more info)

Just tested this with the latest code for both core and entity_browser and I can't reproduce the issue, the modal opens correctly. Can you please test again with a clean Drupal install?

jfrederick’s picture

Issue summary: View changes
Status: Postponed (maintainer needs more info) » Needs work

Thanks for bringing this up! I left out an essential part of reproducing this. In step #3, the number field must be required. I will adjust the Issue Summary accordingly. Given that info, I still see the problem with latest D8 dev and EB dev.

colinstillwell’s picture

I have a similar test case which I wanted to share. The patch on this issue seems to solve the problem for me.

I have a custom entity with some fields on it, one of them being an inline entity form and another being number (required integer).

If I provide a value in my number field it then allows me to use my inline entity reference field without any errors or warnings.

My contrib modules are as follows:
- adminimal_theme
- commerce
- features
- inline_entity_form

swentel’s picture

Issue tags: +Needs tests
colinstillwell’s picture

@swental I am new to the release process for issues. How do we go about running tests? I would like to help out where possible!

swentel’s picture

@colinstillwell88

Well, what we need is a test only patch that proves the bug. We can add a test in NumberFieldTest for it.
We'll also have to double check we don't introduce regressions with the original patch though, so we might have to check other tests or even write more.

We'll also have to update the Issue Summary because the bug is not only about dialogs or so, it's about a required number and any ajax request after that.

I'll probably post a patch tonight already because this is holding me up as well. After I post the patch, we'll need reviewers, people who look at the code, but also confirm it actually works, although we have some confirmations already here and from #2648520: Recoverable fatal error: Argument 1 passed to Drupal\Core\Form\FormState::setError() must be of the type array, null given.

swentel’s picture

Status: Needs work » Needs review
Issue tags: -Needs tests
StatusFileSize
new1.88 KB

Here's a test only patch, will fail horribly.

swentel’s picture

Issue summary: View changes

Status: Needs review » Needs work

The last submitted patch, 9: 2614250-10-test-only.patch, failed testing.

amateescu’s picture

I still can't reproduce this on my local, even after following the steps from the test in #9 :(

swentel’s picture

@amateescu does the test fail on your local machine ?
Maybe PHP version ? Haven't tested myself on PHP 7, will try that today.

amateescu’s picture

Yep, the test fails and I tried to look into it a bit with debug() calls but I won't get anywhere without xdebug.

What I was able to find is only that in \Drupal\Core\Field\WidgetBase::flagErrors(), line 423, $violation->getPropertyPath() returns an empty string, which means that $delta_element = $element; below will be $element[0]['value'] instead of just $element['value'], and that's why the call to $this->errorElement() below will send the wrong form array structure.

swentel’s picture

Wondering if we shouldn't fix this more upstream, it seems like more widgets can trigger this bug.

Maybe in #2027059: Improve the documentation of WidgetBase::errorElement() for mapping violation property paths to form elements ? (although I haven't fully read the patch though and yched has won't fixed it)

amateescu’s picture

Yep, the @todo from the patch in #2553983-3: Required textarea with summary breaks ajax events for other fields. clearly shows that we need to fix the violation property paths :)

the69’s picture

Hi!
I got the same error when adding a new node in the entity reference field with inline entity form module.

Notice: Undefined index: value в Drupal\Core\Field\Plugin\Field\FieldWidget\NumberWidget->errorElement() (line 119 file .../core/lib/Drupal/Core/Field/Plugin/Field/FieldWidget/NumberWidget.php).

Recoverable fatal error: Argument 1 passed to Drupal\Core\Form\FormState::setError() must be of the type array, null given, called in .../core/lib/Drupal/Core/Field/WidgetBase.php on line 446 and defined in Drupal\Core\Form\FormState->setError() (line 1156 file .../core/lib/Drupal/Core/Form/FormState.php).

The patch from #2 @jfrederick helped me! Thank you so much!

sylvainm’s picture

I have the same problem with custom validation.
I think it is better to remove errorElement method, as its parent (WidgetBase) does exactly what patch of #2 does.

Version: 8.0.x-dev » 8.1.x-dev

Drupal 8.0.6 was released on April 6 and is the final bugfix release for the Drupal 8.0.x series. Drupal 8.0.x will not receive any further development aside from security fixes. Drupal 8.1.0-rc1 is now available and sites should prepare to update to 8.1.0.

Bug reports should be targeted against the 8.1.x-dev branch from now on, and new development or disruptive changes should be targeted against the 8.2.x-dev branch. For more information see the Drupal 8 minor version schedule and the Allowed changes during the Drupal 8 release cycle.

artis’s picture

#19 is a good workaround until this is fixed upstream.

xjm’s picture

Priority: Normal » Major

Since this involves unpredictable fatal errors for end users after the site builder assembles a fully supported data model in the UI alone for a common usecase, that is a strong case for this issue to be considered as a major.

zerolab’s picture

#2619878-2: Recoverable fatal error: Argument 1 passed to Drupal\\Core\\Form\\ FormState::setError() must be of the type array uses a different approach than the patch in #19.
I feel that a combined patch would do the trick.

artis’s picture

A combined patch is not possible here. The two approaches are mutually exclusive.

One edits the problematic line of code and the other approach deletes it to allow the class being extended to handle this function.

The later approach keeps us from repeating code.

sylvainm’s picture

Status: Needs work » Needs review
StatusFileSize
new2.53 KB

Ok, here is a patch with the test of swentel (#9)

Thanks for reviewing

hnln’s picture

I had the same problem with an entitybrowser on an entity reference field, once I added number fields, they stopped working. Patch 25 fixed the issue.

zerolab’s picture

Status: Needs review » Reviewed & tested by the community
Issue tags: +dcbristol2016

We have been using #25 on several builds and it fixes the issue.
Includes the test from #9 and it is green.

I think this is RTBC

alexpott’s picture

Shouldn't we be pushing for a generic fix for this... see #2027059: Improve the documentation of WidgetBase::errorElement() for mapping violation property paths to form elements.

+++ b/core/lib/Drupal/Core/Field/Plugin/Field/FieldWidget/NumberWidget.php
@@ -107,11 +107,4 @@ public function formElement(FieldItemListInterface $items, $delta, array $elemen
-  /**
-   * {@inheritdoc}
-   */
-  public function errorElement(array $element, ConstraintViolationInterface $violation, array $form, FormStateInterface $form_state) {
-    return $element['value'];
-  }

I'm not sure that removing this is correct - when the violation is due to the numeric field it should be set on the value element.

I think the correct thing to do here is work on #2027059: Improve the documentation of WidgetBase::errorElement() for mapping violation property paths to form elements and then use this issue to add the missing test coverage if that one does not.

alexpott’s picture

Status: Reviewed & tested by the community » Postponed

Yep reading the issue summary of #2027059: Improve the documentation of WidgetBase::errorElement() for mapping violation property paths to form elements I'm convinced that we should attempt the generic fix and that the fix here is wrong. Postponing on that issue.

artis’s picture

Patch #25 was failing for me on 8.1.8. Same patch against current 8.1.x for those of us using this in production sites, until the generic fix is finished.

UPDATED: Now I think it's just a composer issue on my system. This patch is not necessary.

artis’s picture

Version: 8.1.x-dev » 8.2.x-dev

Drupal 8.1.9 was released on September 7 and is the final bugfix release for the Drupal 8.1.x series. Drupal 8.1.x will not receive any further development aside from security fixes. Drupal 8.2.0-rc1 is now available and sites should prepare to upgrade to 8.2.0.

Bug reports should be targeted against the 8.2.x-dev branch from now on, and new development or disruptive changes should be targeted against the 8.3.x-dev branch. For more information see the Drupal 8 minor version schedule and the Allowed changes during the Drupal 8 release cycle.

nickmans’s picture

subscribing

Version: 8.2.x-dev » 8.3.x-dev

Drupal 8.2.6 was released on February 1, 2017 and is the final full bugfix release for the Drupal 8.2.x series. Drupal 8.2.x will not receive any further development aside from critical and security fixes. Sites should prepare to update to 8.3.0 on April 5, 2017. (Drupal 8.3.0-alpha1 is available for testing.)

Bug reports should be targeted against the 8.3.x-dev branch from now on, and new development or disruptive changes should be targeted against the 8.4.x-dev branch. For more information see the Drupal 8 minor version schedule and the Allowed changes during the Drupal 8 release cycle.

xjm’s picture

Issue tags: +Triaged core major

The core committers and entity and field system maintainers discussed this issue awhile back and agreed it is a major bug because it is a fatal error triggerable through the user interface. It is currently blocked on #2027059: Improve the documentation of WidgetBase::errorElement() for mapping violation property paths to form elements. It may turn out to be a duplicate of that issue, but we should confirm once that issue is resolved.

hyscaler’s picture

Version: 8.3.x-dev » 8.3.1
Status: Postponed » Patch (to be ported)
StatusFileSize
new2.38 KB

Attaching fix for Drupal 8.3.1

alexpott’s picture

Version: 8.3.1 » 8.3.x-dev
Status: Patch (to be ported) » Postponed

@nettantra please read the comments in #29 and #35 this issue is postponed on #2027059: Improve the documentation of WidgetBase::errorElement() for mapping violation property paths to form elements - it would be great if people can work on getting the generic issue fixed. Thanks.

zenimagine’s picture

#36 Works for me with drupal 8.3.4

egruel’s picture

The patch #36 work for me so with drupal 8.3.4.

artis’s picture

StatusFileSize
new2.55 KB

For those of us who need a temporary fix, here is a patch that applies to 8.3.5.

artis’s picture

StatusFileSize
new694 bytes

patch without extra test as temporary fix against 8.3.5

Version: 8.3.x-dev » 8.4.x-dev

Drupal 8.3.6 was released on August 2, 2017 and is the final full bugfix release for the Drupal 8.3.x series. Drupal 8.3.x will not receive any further development aside from critical and security fixes. Sites should prepare to update to 8.4.0 on October 4, 2017. (Drupal 8.4.0-alpha1 is available for testing.)

Bug reports should be targeted against the 8.4.x-dev branch from now on, and new development or disruptive changes should be targeted against the 8.5.x-dev branch. For more information see the Drupal 8 minor version schedule and the Allowed changes during the Drupal 8 release cycle.

amateescu’s picture

Status: Postponed » Closed (duplicate)
traedamatic’s picture

Hello all,

@amateescu, we still get this error on our Drupal installation (8.3.7) with your patch on the other issue #2901943.

Can someone confirm that?

markus_petrux’s picture

@traedamatic: This issue was happening to us too, but the patch in #2901943 fixed it.

bachilli’s picture

StatusFileSize
new666 bytes

Temporary fix until Drupal core team release the full docs of errorElement.

FIX ISSUES w/ Paragraphs project.
FIX ISSUES w/ ajax calls and Number Widgets.

I do not did too many tests, so use carefully.

DRUPAL VERSION: 8.3.7

amateescu’s picture

@bachilli, the proper fix for this bug has already been committed to 8.4.x and should be available in 8.4.1.

bachilli’s picture

@amateescu can you link the commit in this post? thanks!

amateescu’s picture

bachilli’s picture

@amateescu nice! but I've already tried this solution and conflicts with Paragraphs but I dont know why.

amateescu’s picture

@bachilli, you might also want to try another patch that's fixing stuff in the same area: #2784015-22: Widget validation crashes on ItemList violations for widgets with a custom errorElement() implementation

schrammos’s picture

After updating to D8.4.2 (where patch from #2901943 is included), I still had the same error on a required number field.

But after applying the patch from #2784015 (see comment above), the error was gone. So I guess we should add #2784015 as related issue here.